﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageBusinessCategories : System.Web.UI.Page
{

    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_BUSINESSCATEGORY = "__BusinessCategory::BC";
    private const string PROP_LAYOUT = "__BusinessCategory::UIL";
    private const string PROP_SELECTED_BUSINESSCATEGORY_ID = "__BusinessCategory::SBCID";

    #endregion

    #region Public Properties

    public BusinessCategoryCollection DataSourceBusinessCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_BUSINESSCATEGORY] is BusinessCategoryCollection ? (BusinessCategoryCollection)SessionMgr.Session[PROP_DATASRC_BUSINESSCATEGORY] : (BusinessCategoryCollection)(SessionMgr.Session[PROP_DATASRC_BUSINESSCATEGORY] = new BusinessCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_BUSINESSCATEGORY] = value; }
    }

    public int SelectedBusinessCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_BUSINESSCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_BUSINESSCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_BUSINESSCATEGORY_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceBusinessCategory = BusinessCategory.GetAllBusinessCategories();
            BusinessCategoryList1.DataBind(DataSourceBusinessCategory);
        }
    }

    #endregion

    #region Control: BusinessCategoryList

    protected void ucBusinessCategoryList_EditRequested(object sender, Controls_BusinessCategoryList.EditRequestedEventArgs evt)
    {
        SelectedBusinessCategoryID = evt.BC.BusinessCatgryID;
        BusinessCategoryViewEdit1.DataBind(evt.BC);
        SetLayout(LayoutType.Edit);
    }


    protected void ucBusinessCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        BusinessCategoryViewEdit1.CancelEdit();
        BusinessCategory newBusinessCategory = BusinessCategory.CreateNew(DataSourceBusinessCategory);
        SelectedBusinessCategoryID = newBusinessCategory.BusinessCatgryID;
        BusinessCategoryViewEdit1.DataBind(DataSourceBusinessCategory.FindByBusinessCategoryID(SelectedBusinessCategoryID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucBusinessCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceBusinessCategory = BusinessCategory.GetAllBusinessCategories();
        BusinessCategoryList1.DataSource = DataSourceBusinessCategory;
    }

    #endregion

    #region Control: BusinessCategoryViewEdit

    protected void ucBusinessCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        BusinessCategoryViewEdit1.DataSourceBusinessCategory = DataSourceBusinessCategory.FindByBusinessCategoryID(SelectedBusinessCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        BusinessCategoryViewEdit1.CancelEdit();
        // If we were adding a new Businesscategory...then remove the incomplete/not added new Businesscategory object from the collection.
        if (DataSourceBusinessCategory.FindByBusinessCategoryID(SelectedBusinessCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceBusinessCategory.Remove(DataSourceBusinessCategory.FindByBusinessCategoryID(SelectedBusinessCategoryID));
                SelectedBusinessCategoryID = (DataSourceBusinessCategory[0] != null ? DataSourceBusinessCategory[0].BusinessCatgryID : 0);
            }
            catch
            {
            }
        }
        BusinessCategoryList1.DataBind(DataSourceBusinessCategory);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {

        if (BusinessCategoryViewEdit1.EndEdit())
        {

            BusinessCategory obj = DataSourceBusinessCategory.FindByBusinessCategoryID(SelectedBusinessCategoryID);
            BusinessCategory.Update(obj);

            // TODO: Set Feedback text...

            BusinessCategoryList1.DataBind(DataSourceBusinessCategory);
            SetLayout(LayoutType.View);

        }
        else
        {
            divexistbusinesscategoryname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlBusinessCategoryList.Visible = false;
                pnlBusinessCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlBusinessCategoryList.Visible = true;
                pnlBusinessCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion

    
}
